package cn.jdy.task.service;

import cn.jdy.common.constant.GlobalConstant;
import cn.jdy.framework.log.BaseLog;
import cn.jdy.framework.util.PropertyUtil;
import cn.jdy.task.entity.SchedulingJob;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by Tianyi on 2014/8/8.
 */
@Service
public class InitBean implements InitializingBean {

	@Resource
	private SchedulerService schedulerService;
	/**
	 * 项目启动时，初始化定时任务
	 */
	public void afterPropertiesSet() throws Exception {
		//判断定时任务开关是否打开
		if("on".equals(PropertyUtil.getProInfo(GlobalConstant.PRO_FILE_CONFIG, "taskExecute"))) {
			String taskOwer = PropertyUtil.getProInfo(GlobalConstant.PRO_FILE_CONFIG, "taskOwer");
			try {
				if(taskOwer!=null && taskOwer.length()>0) {
					List<SchedulingJob> list = schedulerService.find("status", "0", "ower", taskOwer);
					for(SchedulingJob schedulingJob : list) {
						try {
							schedulerService.startJob(schedulingJob);
						}catch (Exception e) {
							BaseLog.e(this.getClass(), taskOwer + "定时任务初始化启动失败！", e);
						}
					}
					BaseLog.i(this.getClass(), taskOwer + "定时任务初始化启动成功！");
				}else {
					BaseLog.i(this.getClass(), "taskOwer为空，定时任务无法启动！");
				}
			} catch (Exception e) {
				BaseLog.e(this.getClass(), taskOwer + "定时任务初始化启动失败！", e);
			}
		}
	}
}
